export const state = () => {
  return {
    index: {
      banners: [],
      pageSize: 20,
      pageNum: 1,
      count: 0,
      listData: []
    }
  }
}

export const getters = {
  // indexListData: state => state.index.listData
}

export const mutations = {
  // 设置首页banner
  SET_INDEX_BANNER (state, data) {
    state.index.banners = data.banners
  },
  // 获取首页列表数据,
  SET_INDEX_PAGE (state, pageNum) {
    state.index.pageNum = pageNum
  },
  SET_INDEX_LISTDATA (state, data) {
    if (data.currentPage == 1) {
      state.index.count = data.count
      state.index.listData = data.data
    } else {
      for (const i in data.data) {
        state.index.listData.push(data.data[i])
      }
    }
  }
}

export const actions = {
  GET_INDEX_LISTDATA ({commit, state}) {
    return this.$axios.$get('/blog/allarticle', {
      params: {
        pageSize: state.index.pageSize,
        pageNum: state.index.pageNum
      }
    }).then(res => {
      if (!res.errno) {
        commit('SET_INDEX_LISTDATA', res.data)
      }
    })
  },
  // 获取首页banner
  GET_INDEX_BANNERS ({commit, state}) {
    return this.$axios.$get('/blog/banners/index').then(res => {
      if (!res.errno) {
        commit('SET_INDEX_BANNER', res.data)
      }
    })
  }

}